<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增学生信息')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-select-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-student-add">

            <div class="form-group">    
                <label class="col-sm-3 control-label">学生姓名：</label>
                <div class="col-sm-8">
                    <input name="studentName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">年级：</label>
                <div class="col-sm-8">
                    <input id="gradeName" name="gradeName"  type="hidden">
                    <select class='form-control selectpicker' id='gradeId' name='gradeId'>
                    </select>
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label">班级名称：</label>
                <div class="col-sm-8">
                    <input id="className" name="className"  type="hidden">
                    <select class='form-control selectpicker' data-live-search="true" id='classId' name='classId'>
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label">家长电话：</label>
                <div class="col-sm-8">
                    <input name="parentsMobile" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">家长姓名：</label>
                <div class="col-sm-8">
                    <input name="parentsName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">住址：</label>
                <div class="col-sm-8">
                    <input name="address" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">入学时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="comeDate" class="form-control" placeholder="yyyy-MM-dd" type="text">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">照片：</label>
                <div class="col-sm-8">
                    <input type="hidden"  name="img" id="img_url">
                    <div class="file-loading">
                        <input type="file"  name="file" id="img">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">年龄：</label>
                <div class="col-sm-8">
                    <input name="age" class="form-control" placeholder="请输入年龄" type="number">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">性别：</label>
                <div class="col-sm-8">
                    <select name="sex" class="form-control m-b" th:with="type=${@dict.getType('sys_user_sex')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">入学分数：</label>
                <div class="col-sm-8">
                    <input name="comeScore" class="form-control" type="number">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">考号：</label>
                <div class="col-sm-8">
                    <input name="examCode" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">爱好：</label>
                <div class="col-sm-8">
                    <textarea name="hobbies" maxlength="200" class="form-control" rows="3"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">目标：</label>
                <div class="col-sm-8">
                    <textarea name="target" maxlength="200" class="form-control" rows="3"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">座右铭：</label>
                <div class="col-sm-8">
                    <textarea name="motto" maxlength="200" class="form-control" rows="3"></textarea>
                </div>
            </div>


        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
        var prefix = ctx + "school/student"
        $("#form-student-add").validate({
            focusCleanup: true
        });

        function submitHandler() {

            $("#gradeName").val($("#gradeId option:selected").text());

            $("#className").val($("#classId option:selected").text());
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-student-add').serialize());
            }
        }

        $("input[name='comeDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='leaveDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });


        $(document).ready(function(){

            $("#img").fileinput({
                'theme': 'explorer-fas',
                'uploadUrl': '/common/upload',
                'overwriteInitial': false,
                'maxFileCount':1,
                'showUpload':false


            }).on("filebatchselected", function(event, files) { // 选择文件后回调
                $("#img").fileinput("upload");
            }).on("fileuploaded", function(event, data, previewId, index) { // 上传成功回调
                if(data.response.code == 0){
                    $("#img_url").val(data.response.url);
                }
            });



            $.ajax({
                type: "post",
                url:ctx + "school/grade/getGradeList",
                async:false,
                dataType: "json",
                success: function(res){
                    if (null != res) {
                        var html = '<option value="">请选择</option>';
                        Object.keys(res).forEach(function(key){
                            html += '<option value="' + res[key].id + '" data-tokens="'+ res[key].gradeName+'">' + res[key].gradeName+ '</option>';
                        });
                        $("#gradeId").html(html);
                        //必须加，刷新select
                        $("#gradeId").selectpicker('refresh');
                    }
                }
            });
            $('#gradeId').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
                // 处理自己的业务
                if($(this).val() != undefined){
                    //绑定bootstrap-select事件
                    $.ajax({
                        type: "post",
                        url:ctx + "school/classInfo/getClassList?gradeId="+$(this).val(),
                        async:false,
                        dataType: "json",
                        success: function(res){
                            if (null != res) {
                                var html = '<option value="">请选择</option>';
                                Object.keys(res).forEach(function(key){
                                    html += '<option value="' + res[key].id + '" data-tokens="'+ res[key].className+'">' + res[key].className+ '</option>';
                                });
                                $("#classId").html(html);
                                //必须加，刷新select
                                $("#classId").selectpicker('refresh');
                            }
                        }
                    });
                }
            });

        });



    </script>
</body>
</html>